export const VueLazyLoad = {
    inserted(el, bind) {
        const bgc = el.style.backgroundColor;
        el.style.color = '#fff';
        el.style.backgroundColor = '#f2f2f2';
        const io = new IntersectionObserver(entries => {
            entries.forEach(entry => {
                if (entry.intersectionRatio > 0) {
                    el.style.color = 'inhert';
                    el.style.backgroundColor = bgc;
                    entry.target.setAttribute('src', bind.value);
                    io.unobserve(entry.target); // 移除监听
                }
            })
        })
        io.observe(el); // 添加一个监听
    },
    update() {
        // TODO
    }
}

export default {
    install(Vue) {
        Vue.directive('lazyload', VueLazyLoad);
    }
}
